/* 
 * File:   Box.cpp
 * Author: brady
 * 
 * Created on September 8, 2011, 11:06 PM
 */

#include <shapes/Box.h>

Box::Box() {
}

Box::Box(const Box& orig) {
}

Box::Box(const Vector3D& origin, const Vector3D& size){
    double x, y, z, w, l, h;
    x=origin[0];
    y=origin[1];
    z=origin[2];
    w=size[0];
    h=size[1];
    l=size[2];
    double wh, lh, hh;
    wh = w/2.0;
    lh = l/2.0;
    hh = h/2.0;
    Vertex *tlf, *blf, *trf, *brf, *tlb, *blb, *trb, *brb;
    tlf = new Vertex(Vector3D(x-wh, y+hh, z-lh));
    blf = new Vertex(Vector3D(x-wh, y-hh, z-lh));
    trf = new Vertex(Vector3D(x+wh, y+hh, z-lh));
    brf = new Vertex(Vector3D(x+wh, y-hh, z-lh));
    tlb = new Vertex(Vector3D(x-wh, y+hh, z+lh));
    blb = new Vertex(Vector3D(x-wh, y-hh, z+lh));
    trb = new Vertex(Vector3D(x+wh, y+hh, z+lh));
    brb = new Vertex(Vector3D(x+wh, y-hh, z+lh));
    //front face
    faces.addTriangle(tlf, trf, brf);
    faces.addTriangle(tlf, brf, blf);
    //left side
    faces.addTriangle(tlb, blf, blb);
    faces.addTriangle(tlb, tlf, blf);
    //right side
    faces.addTriangle(trb, brb, brf);
    faces.addTriangle(trb, brf, trf);
    //back face
    faces.addTriangle(tlb, brb, trb);
    faces.addTriangle(tlb, blb, brb);
    //top
    faces.addTriangle(tlf, tlb, trb);
    faces.addTriangle(tlf, trb, trf);
    //bottom
    faces.addTriangle(blf, brb, blb);
    faces.addTriangle(blf, brf, brb);
    
    faces.setShader(NULL);
    faces.finalize();
    boundingBox = faces.getBoundingBox();
}

Box::~Box() {
}

bool Box::testIntersect(IntersectionParams& ip, IntersectionResult& ir){
    bool hit = faces.testIntersect(ip, ir);
    if(hit){
        ir.shape = this;
    }
    return hit;
}
